package net.cloudcodex.client.campaign.presenter;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import net.cloudcodex.client.CloudCodexEventBus;
import net.cloudcodex.client.campaign.view.CampaignConfigView;
import net.cloudcodex.client.campaign.view.CampaignConfigViewImpl;
import net.cloudcodex.client.campaign.view.CampaignsView.Message;
import net.cloudcodex.client.character.handler.TextBoxBaseKeyHandler;
import net.cloudcodex.client.util.ClientUtils;
import net.cloudcodex.client.util.rpc.SuccessCallback;
import net.cloudcodex.shared.dto.Result;
import net.cloudcodex.shared.dto.home.CampaignSummaryDTO;
import net.cloudcodex.shared.rpc.CampaignRPCAsync;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;
import com.mvp4g.client.annotation.Presenter;
import com.mvp4g.client.presenter.BasePresenter;

@Presenter(view = CampaignConfigViewImpl.class, multiple = true)
public class CampaignConfigPresenter extends
		BasePresenter<CampaignConfigView, CloudCodexEventBus> {
	private final Message message;
	private CampaignSummaryDTO campaignSummaryDTO;
	private TextBoxBaseKeyHandler noteTextAreaKeyHandler;
	private Set<String> modifiedTextAreaTitleSet = new HashSet<String>();
	private CampaignRPCAsync campaignService;

	@Inject
	public CampaignConfigPresenter(Message pMessage,CampaignRPCAsync campaignService) {
		this.message = pMessage;
		this.campaignService = campaignService;
	}

	@Override
	public void bind() {

		noteTextAreaKeyHandler = new TextBoxBaseKeyHandler();
		noteTextAreaKeyHandler.setEventBus(eventBus);
		this.view.getDescriptionTexteAreaKeyUp().addKeyUpHandler(
				noteTextAreaKeyHandler);
		this.view.getDescriptionTexteAreaKeyDown().addKeyDownHandler(
				noteTextAreaKeyHandler);
		this.view.getNameTextBoxKeyUp().addKeyUpHandler(noteTextAreaKeyHandler);
		this.view.getNameTextBoxKeyDown().addKeyDownHandler(
				noteTextAreaKeyHandler);
		this.view.getGameTextBoxKeyDown().addKeyDownHandler(
				noteTextAreaKeyHandler);
		this.view.getGameTextBoxKeyUp().addKeyUpHandler(noteTextAreaKeyHandler);
		this.view.getIconTextBoxKeyDown().addKeyDownHandler(
				noteTextAreaKeyHandler);
		this.view.getIconTextBoxKeyUp().addKeyUpHandler(noteTextAreaKeyHandler);

		this.view.getModifyButton().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				
				
				String name = getValue(view.getCampaignName());
				String game = getValue(view.getCampaignGame());
				String icon = getValue(view.getCampaignIcon());
				String description = getValue(view.getCampaignDescription());
				campaignService.updateCampaign(campaignSummaryDTO.getId(), name, game, description, icon, new SuccessCallback<Result<CampaignSummaryDTO>>() {

					@Override
					public void onSuccess(
							Result<CampaignSummaryDTO> result) {
						if (!result.hasErrors()) {
							modifiedTextAreaTitleSet.clear();
							eventBus.updateCampaign(result.getData());
							setUpdateButtonEnable(false);
						}
					}
				});
				
				System.out.println("campaign.updateCampaign() not implemented" );
				
				
			}
			private String getValue(String text) {

				if (!ClientUtils.isBlankOrNull(text)) {
					return text;
				}
				return null;
			}
		});

	}

	public void addCampaignSummaryDTO(CampaignSummaryDTO campaignSummaryDTO) {
		this.campaignSummaryDTO = campaignSummaryDTO;
	}

	public void initView() {

		this.view.addCampaignInfos(campaignSummaryDTO.getName(),
				campaignSummaryDTO.getIcon(), campaignSummaryDTO.getGame(),
				campaignSummaryDTO.getCreationDate());
		this.view.initView();

	}
	public void refreshCampaignInfos(){
		this.view.refreshCampaignInfos(campaignSummaryDTO.getName(),
				campaignSummaryDTO.getIcon(), campaignSummaryDTO.getGame());
	}
	
	public void onUpdateButtonEnable(boolean isEnable, String textAreaID) {
		if (isEnable) {
			modifiedTextAreaTitleSet.add(textAreaID);
		} else {
			modifiedTextAreaTitleSet.remove(textAreaID);
		}
		setUpdateButtonEnable(!modifiedTextAreaTitleSet.isEmpty());
	}

	private void setUpdateButtonEnable(boolean enable) {
		view.setModifyButtonEnable(enable);
	}
}
